import router from './router'
import store from './store'
import {getToken} from './utils/auth' // 验权
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css'// Progress 进度条样式
import {Toast} from 'vant';

// 不重定向白名单
// const whiteList = ['/login', '/', '/home', '/category'];
const whiteName = ['product']

router.beforeEach((to, from, next) => {
    NProgress.start()
    let token = getToken();
    if (token) {
        if (to.path === '/login') {
            next('/')
            NProgress.done()
        } else {
            store.dispatch('GetInfo').then(res => {
                next();
                NProgress.done();
            })
        }
    } else {
        if (to.meta.requireAuth) {
            Toast.fail({
                message: '请先登录'
            });
            setTimeout(() => {
                next("/login")
                NProgress.done()
            }, 2000)
        }else {
            next();
        }
    }
})

router.afterEach(() => {
    NProgress.done()
})